package cn.com.surker.production.service.impl;

import cn.com.surker.enums.StateEnum;
import cn.com.surker.oa.entity.OaProductionContract;
import cn.com.surker.production.entity.TranConCar;
import cn.com.surker.production.dao.TranConCarDao;
import cn.com.surker.production.service.TranConCarService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import cn.com.surker.spring.RestResultDto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * <p>
 * 生产合同关联车辆表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-09-04
 */
public abstract class TranConCarBaseImpl extends ServiceImpl<TranConCarDao, TranConCar> implements TranConCarService {

	@Resource
	protected TranConCarDao tranConCarDao;

	@Override
	public List<TranConCar> queryByConDev(Long conId, Long carId) {
		QueryWrapper<TranConCar> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		if (conId != null) {
			qw.eq("pro_con_id", conId);
		}
		if (carId != null) {
			qw.eq("tran_car_id", carId);
		}
		List<TranConCar> tranConCars = tranConCarDao.selectList(qw);
		return tranConCars;
	}

	/**
	 * 根据合同和车辆查询关联关系
	 * @param conId
	 * @param code
	 * @param takeTime
	 * @return
	 */
	public List<TranConCar> queryByConDevTakeTime(Long conId, String code, String takeTime) {
		QueryWrapper<TranConCar> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		if (conId != null) {
			qw.eq("pro_con_id", conId);
		}
		if (!StringUtils.isNull(code)) {
			qw.eq("code", code);
		}
		if (!StringUtils.isNull(takeTime))
			qw.eq("start_time", takeTime);
		List<TranConCar> tranConCars = tranConCarDao.selectList(qw);
		return tranConCars;
	}

	@Override
	public List<OaProductionContract> findConList(Long orgId, Long carId) {
		List<OaProductionContract> list = tranConCarDao.findConList(orgId, carId);
		return list;
	}

	@Override
	protected Class<TranConCar> currentModelClass() {
		return TranConCar.class;
	}

	/**
	 * 分页查询车辆关联信息
	 * @param searchVal
	 * @param contractId
	 * @param modelType
	 * @param status
	 * @param offset
	 * @param limit
	 * @return
	 */
	public IPage findPage(String searchVal, Long contractId, Integer modelType, Integer status, Integer offset, Integer limit) {
		PageHelper.offsetPage(offset, limit);
		if (StringUtils.isNull(status))
			status = 0;
		if (StringUtils.isNull(searchVal))
			searchVal = null;
		IPage iPage = tranConCarDao.findPageVo(new Page<>((offset + limit) /limit, limit),
				searchVal, contractId, modelType, status);
		/**分页插件**/
		if (!StringUtils.isNull(iPage.getRecords())) {
			PageInfo pageInfo = new PageInfo(iPage.getRecords());
			if (!StringUtils.isNull(pageInfo.getTotal()))
				iPage.setTotal(pageInfo.getTotal());
		}
		return iPage;
	}


	@Override
	public TranConCar queryLink(Long orgId, Long carId, String tranTime) {
		QueryWrapper<TranConCar> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.eq("org_id", orgId);
		//	起始时间要小于等于运输时间
		qw.le("start_time", tranTime);
		qw.and(e -> {
			//	截止时间大于运输时间
			e.isNull("end_time").or().gt("end_time", tranTime);
		});
		qw.eq("tran_car_id", carId);
		qw.last("limit 1");
		TranConCar tranConCar = tranConCarDao.selectOne(qw);
		return tranConCar;
	}
}